package javatest;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core;
import com.vpx.VpxDecoder;
import com.vpx.VpxEncoder;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

/**
 * @author Dmitry
 */
public class TestDecoder {

    public static void mem_put_le16(byte[] mem, int offset, int val) {
        mem[0 + offset] = (byte) val;
        mem[1 + offset] = (byte) (val >> 8);
    }

    public static void mem_put_le32(byte[] mem, int offset, int val) {
        mem[0 + offset] = (byte) val;
        mem[1 + offset] = (byte) (val >> 8);
        mem[2 + offset] = (byte) (val >> 16);
        mem[3 + offset] = (byte) (val >> 24);
    }

    private static void writeFileHeader(RandomAccessFile file, int width, int height) throws IOException {
        byte[] header = new byte[32];
        header[0] = 'D';
        header[1] = 'K';
        header[2] = 'I';
        header[3] = 'F';
        mem_put_le16(header, 4, 0);                   // version i
        mem_put_le16(header, 6, 32);                  // headersize 
        mem_put_le32(header, 8, 0x30385056);              // headersize 
        mem_put_le16(header, 12, width);            // width
        mem_put_le16(header, 14, height);            // height
        mem_put_le32(header, 16, 30); // rate 
        mem_put_le32(header, 20, 1); // scale
        mem_put_le32(header, 24, 200);           // length 
        mem_put_le32(header, 28, 0);                   // unused 
        file.write(header);
    }

    private static byte[] createCopy(byte[] bytes, int offset, int lenght) {
        byte[] newBytes = Arrays.copyOfRange(bytes, offset, offset + lenght);
        return newBytes;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        Utils.loadLibraries();
        int width = 320;
        int height = 240;
        System.out.println("Width:" + width + " Height:" + height);
        VpxEncoder encoder = new VpxEncoder(width, height, VpxEncoder.calculateBasicFrameRate(width, height), 40);
        VpxDecoder decoder = new VpxDecoder(width, height);
        FrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.setImageWidth(width);
        grabber.setImageHeight(height);
        grabber.start();
        opencv_core.IplImage tim = grabber.grab();
        CanvasFrame frame = new CanvasFrame("");
        frame.setDefaultCloseOperation(CanvasFrame.EXIT_ON_CLOSE);
        frame.setSize(grabber.getImageWidth(), grabber.getImageHeight());
        frame.setVisible(true);

        opencv_core.IplImage decodedImage = opencv_core.IplImage.createCompatible(tim);
        BufferedImage image = decodedImage.getBufferedImage();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        try {
            for (int i = 0; i < 200; i++) {
                opencv_core.IplImage camPic = grabber.grab();
                encoder.encodeImage(camPic, outStream);
                byte[] bytes = outStream.toByteArray();
                decoder.decodeImage(decodedImage, bytes);

                frame.showImage(decodedImage);

                System.out.println("Wr Buffer size=" + outStream.size());
                outStream.reset();
            }
        } finally {
            grabber.stop();
        }

        grabber.stop();
        System.exit(0);
    }
}
